from hdfs import InsecureClient
from flask import current_app

# HDFS 地址和用户（根据你自己的改）
client = InsecureClient('http://master:9870', user='root')

def read_hdfs_file(file_path):
    """
    读取 HDFS 文件内容并返回字符串
    """
    try:
        with client.read(file_path) as reader:
            return reader.read().decode('utf-8')
    except Exception as e:
        return f"读取失败: {e}"

def write_hdfs_file(file_path, content):
    """
    写入内容到HDFS文件
    """
    try:
        # 如果是字节内容，需要解码
        if isinstance(content, bytes):
            content = content.decode('utf-8')

        with client.write(file_path, encoding='utf-8', overwrite=True) as writer:
            writer.write(content)
        return True
    except Exception as e:
        current_app.logger.error(f"HDFS write error: {str(e)}")
        return False

def list_hdfs_dir(dir_path):
    """
    列出HDFS目录内容
    """
    try:
        return client.list(dir_path)
    except Exception as e:
        current_app.logger.error(f"HDFS list error: {str(e)}")
        return []

# if __name__ == '__main__':
#     # 测试读取 HDFS 文件
#     file_path = '/zhenti_data/2023年考研英语一真题.txt'  # 换成你 HDFS 中的真实路径
#     content = read_hdfs_file(file_path)
#     print("读取结果如下：")
#     print(content)
